<!-- 合同管理列表 -->
<template>
	<view>
		<bg-search @search="onSearch"></bg-search>
		<bg-tab :tabs='tabs' :current="tabCurrent" @tab-change="onTabChange"></bg-tab>
		<view class="mt-2 font-12 bg-white" v-if="dataList.length > 0">
			<view class="font-12" v-if="tabCurrent == 0">
				<view v-for="(item, index) in dataList" :key="index">
					<view style="font-size: 12px;font-weight: bold;padding: 12px 0 0 24rpx;box-sizing: border-box;">
						{{ item.projectName }}</view>
					<bg-cell2 :show-arrow="true" v-for="(p, i) in item.userContractList" :key="i"
						:border="i < dataList.length - 1">
						<template slot="label">
							<view>
								<view> {{ p.userName }}（{{ p.userId }}）<text class="font-gray">未签署</text></view>
							</view>
						</template>
						<template slot="value">
							<view @click="gotoDetail(p)" class="font-12 font-red">去签署</view>
						</template>
					</bg-cell2>
				</view>
				<!-- <bg-cell2 :show-arrow="true" v-for="(p, i) in dataList" :key="i" :border="i < dataList.length - 1">
					<template slot="label">
						<view>
							<view> {{ p.userName }}（{{ p.userId }}）<text class="font-gray">未签署</text></view>
						</view>
					</template>
					<template slot="value">
						<view @click="gotoDetail(p)" class="font-12 font-red">去签署</view>
					</template>
				</bg-cell2> -->
			</view>
			<view v-else-if="tabCurrent == 1">
				<view v-for="(item, index) in dataList" :key="index">
					<view style="font-size: 12px;font-weight: bold;padding: 12px 0 0 24rpx;box-sizing: border-box;">
						{{ item.projectName }}</view>
					<bg-cell2 :show-arrow="true" v-for="(p, i) in item.userContractList" :key="i"
						:border="i < dataList.length - 1" @click="gotoCheck(p.signUrl)">
						<template slot="label">
							<view>
								<view> {{ p.userName }}（{{ p.userId }}）<text class="">已签署</text></view>
							</view>
						</template>
					</bg-cell2>
				</view>
			</view>
		</view>
		<u-empty v-else></u-empty>
	</view>
</template>

<script>
import { userContracts, signContracts } from '@/api/system/user';
export default {
	onPullDownRefresh() {
		this.onSearch('')
		setTimeout(function () {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	data() {
		return {
			dataList: [],
			params: {
				"projectId": this.$store.getters.projectId,
				"status": 0,  //0未签署1已签署
				"userName": ''
			},
			tabs: ["未签署", "已签署"],
			tabCurrent: 0,
		}
	},
	mounted() {
		this.onSearch('')
	},
	methods: {
		onSearch(e) {
			this.params.userName = e;
			this.getData()
		},
		onTabChange(idx) {
			this.tabCurrent = idx;
			this.params.status = idx;
			this.getData()
		},
		getData() {
			if (!this.params.projectId && !this.$auth.hasRole('super')) {
				return false
			}
			userContracts(this.params).then(res => {
				this.dataList = res.data
			})
		},
		gotoDetail(p) {
			// console.log('p--', p);
			if (p.userId !== this.$store.getters.userId) return this.$modal.msg("不能签署他人合同")
			signContracts(p).then(res => {
				let url = res.data.signUrl
				this.gotoCheck(url)
			})
		},
		gotoCheck(signUrl) {
			uni.navigateTo({
				url: "/pages/common/webview/index?url=" + encodeURIComponent(signUrl) + "&title=合同签约"
			})
		}
	}
}
</script>

<style></style>
